package com.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName:CompletableFutureDemo1
 * Description:
 *
 * @Author kay
 * @Create 2024/5/17 10:57
 * @Version 1.0
 */
//supplyAsync 可以支持返回值
//计算完成时回调方法 whenComplete : 是执行当前任务的线程执行继续执行 whenComplete 的任务。
public class CompletableFutureDemo3 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        System.out.println("main begin...");

        //CompletableFuture创建异步对象
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() ->{
            System.out.println("当前线程：" + Thread.currentThread().getName());
            int result = 1024/0;
            System.out.println("result：" + result);
            return result;
        },executorService).whenComplete((rs,exception) ->{
            System.out.println("rs:" + rs);
            System.out.println("exception:" + exception);
        });
        System.out.println("main over...");
    }
}
